// packageA/pages/article.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        category:[],
        chooseIndex:0,
        type:'article',
        article:[]
    },
    choose(e){
        let ind = e.currentTarget.dataset.index
        this.setData({
            chooseIndex:ind
        })
        this.getArticleList(this.data.category[ind].id)
    },
    getCategory(){
        let that = this
        wx.request({
          url: 'https://guanhe.work/getCategory',
          success(res){
              if(res.data.code == 200){
                that.setData({
                    category:res.data.data
                })
                that.getArticleList(res.data.data[0].id)
              }
                wx.hideLoading({
                    success: (res) => {},
                  })
              
          },
          fail(e){
              wx.hideLoading({
                success: (res) => {},
              })
          }
        })
    },
    goDetail(e){
        wx.navigateTo({
          url: '/packageA/pages/articleDetail?id='+e.currentTarget.dataset.id,
        })
    },
    getArticleList(id){
        let that = this
        wx.request({
            url: 'https://guanhe.work/getArticleList',
            data:!id?{
                type:that.data.type
            }:{
                type:that.data.type,
                categoryId:id 
            },
            success(res){
                if(res.data.code == 200){
                    that.setData({
                        article:res.data.data
                    })
                }
                    wx.hideLoading({
                        success: (res) => {},
                      })
                  
            },
            fail(e){
                wx.hideLoading({
                  success: (res) => {},
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.showLoading({
          title: '请稍等...',
        })
        let that = this
        that.setData({
            type:options.type
        })
        if(options.type=="api"){
            that.getCategory()
        } else {
            that.getArticleList()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})